package com.atguigu.book.dao.impl;

import com.atguigu.book.dao.CartItemDAO;
import com.atguigu.book.pojo.CartItem;
import com.atguigu.book.pojo.User;
import com.atguigu.myssm.basedao.BaseDAO;

import java.util.List;

/**
 * @ClassName: CartItemDAOImpl
 * @Description:
 * @Author: wty
 * @Date: 2022/12/13
 */

public class CartItemDAOImpl extends BaseDAO<CartItem> implements CartItemDAO {
    @Override
    public void addCartItem(CartItem cartItem) {
        String sqlStr = "INSERT INTO bookdb.`t_cart_item` VALUES(0,?,?,?)";
        executeUpdate(sqlStr, cartItem.getBook().getId(), cartItem.getBuyCount(), cartItem.getUserBean().getId());
    }

    @Override
    public void updateCartItemBuyCount(CartItem cartItem) {
        String sqlStr = "UPDATE bookdb.`t_cart_item` SET buyCount = ? WHERE id = ?";
        executeUpdate(sqlStr, cartItem.getBuyCount(), cartItem.getId());

    }

    @Override
    public List<CartItem> getCartItemList(User user) {
        String sqlStr = "SELECT * FROM bookdb.`t_cart_item` a WHERE a.userBean = ?";
        return executeQuery(sqlStr, user.getId());
    }

    @Override
    public void delCartItem(CartItem cartItem) {
        String sqlStr = "DELETE FROM bookdb.`t_cart_item` WHERE id = ?";
        executeUpdate(sqlStr, cartItem.getId());
    }
}
